package com.hopu.oa.controller.process;


import com.hopu.oa.dao.process.NewProcessDao2;
import com.hopu.oa.entity.management.AoaTypeList;
import com.hopu.oa.entity.process.*;

import com.hopu.oa.entity.user.AoaUser;
import com.hopu.oa.service.process.NewProcessService2;
import com.hopu.oa.service.process.auditService;
import com.hopu.oa.until.OosUtil;
import com.hopu.oa.until.data.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: NewProcessController
 * @Description:
 * @Date: 2021/11/22 19:18
 */

@Controller
public class NewProcessController {


    //工具类，文件上传
    @Autowired
    private OosUtil oosUtil;

    //service
    @Autowired
    private NewProcessService2 newProcessService2;

    //张佑威的(通讯录)
    @Autowired
    private auditService auditService;

    @Autowired
    private NewProcessDao2 newProcessDao2;





/*

 ########################       1.费用报销       ####################################

*/

    @RequestMapping("burse")
    public String burse(HttpSession session, Model model) {


        //  紧急程度
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");
        model.addAttribute("harrylist", status);

        //用户名
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());

        //Type
        List<AoaTypeList> status2 = newProcessService2.queryType("报销");
        model.addAttribute("uplist", status2);


        //通讯录
        addressList(model, session);


        //查询费用科目
        List<AoaSubject> aoaSubjects = newProcessService2.querySubject();
        model.addAttribute("second", aoaSubjects);


        return "process/bursement";
    }


    /*
      @Description : 报销
      @Date   :  2021/11/24
      @Param  : [session, proId_processName, proId_deeply, shenuser, proId_processDescribe, bursement, file, produceTime, subject, descript, invoices, detailmoney]
      @return : java.lang.String
     */
    @RequestMapping("apply")
    public String apply
    (HttpSession session,
     String proId_processName,
     String proId_deeply,
     String shenuser,
     String proId_processDescribe,
     AoaBursement bursement,
     String name,
     String namemoney,
     @RequestParam("filePath") MultipartFile file    //资料

    ) {
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        AoaProcessList process = new AoaProcessList();

        if (file.getSize() != 0) {
            //url
            String shuffix = oosUtil.getPicSuffix(file.getOriginalFilename());
            long size = file.getSize();
            String type = file.getContentType();
            AoaAttachmentList attachment = new AoaAttachmentList();
            attachment.setAttachmentName(file.getOriginalFilename());
            attachment.setAttachmentPath(oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file)));
            attachment.setAttachmentSize(size + "");
            attachment.setAttachmentType(type);
            attachment.setAttachmentShuffix(shuffix);
            attachment.setModel("note");
            attachment.setUploadTime(DateUtils.getDateTimeString());
            attachment.setUserId(aoaUser.getUserId() + "");
            newProcessService2.inertAttachment(attachment);
            System.out.println(attachment.getAttachmentId());
            process.setProFileId(attachment.getAttachmentId());
            System.out.println("ppID = " + process.getProFileId());

        }


        Integer money = 0;
        Integer invoice = 0;

       /*
         获取总金额
       */
        for (Details d : bursement.getDetails()
        ) {
            money += Integer.parseInt(d.getDetailmoney());
            invoice += Integer.parseInt(d.getInvoices());
        }


        System.out.println(proId_processName);
        System.out.println(proId_deeply);
        System.out.println(shenuser);
        System.out.println(proId_processDescribe);
        System.out.println(bursement);


        String[] shenuserName = shenuser.split(";");


        //总表
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setDeeplyId(Integer.parseInt(proId_deeply));
        process.setProcessName(proId_processName);
        process.setProcessDes(proId_processDescribe);
        process.setIsChecked(0);
        process.setStatusId(23);
        process.setTypeName("费用报销");
        process.setProcessUserId(aoaUser.getUserId());
        process.setShenuser(shenuser);
        newProcessService2.insertProcess(process);

        //报销表
        bursement.setAllMoney(money);
        bursement.setAllinvoices(invoice);
        bursement.setBurseTime(DateUtils.getDateTimeString());
        bursement.setManagerAdvice("");
        bursement.setOperationName(aoaUser.getUserId());
        bursement.setProId(process.getProcessId());
        bursement.setUserName(newProcessDao2.queryUserIdByName(namemoney));
        bursement.setName(name);
        newProcessService2.inertBursement(bursement);


        AoaReviewed reviewed = new AoaReviewed();
        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }


        //细节表
        for (int i = 0; i < bursement.getDetails().size(); i++) {
            Details details = bursement.getDetails().get(i);

            details.setBursmentId(bursement.getBursementId());

            newProcessService2.insertDetails(details);
        }


        return "redirect:/xinxeng";
    }





/*
 #########################        2.出差        ###################################
 */

    @RequestMapping("evection")
    public String evection(HttpSession session, Model model) {

        //  紧急程度
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");
        model.addAttribute("harrylist", status);

        //用户名
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());

        //Type
        List<AoaTypeList> status2 = newProcessService2.queryType("出差");
        model.addAttribute("outtype", status2);


        //通讯录
        addressList(model, session);
        return "process/evection";

    }

    /*
      @Description :出差
      @Date   :  2021/11/24
      @Param  : [session, proId_processName, proId_deeply, typeId, proId_startTime, proId_endTime, proId_processDescribe, shenuser, file]
      @return : java.lang.String
     */
    @RequestMapping("evec")
    public String evec(HttpSession session,
                       String proId_processName,
                       String proId_deeply,
                       String typeId,
                       String proId_startTime,
                       String proId_endTime,
                       String proId_processDescribe,
                       String shenuser,
                       @RequestParam("filePath") MultipartFile file     //资料
    ) {
        System.out.println(proId_processName);
        System.out.println(proId_deeply);
        System.out.println(typeId);
        System.out.println(proId_startTime);
        System.out.println(proId_endTime);
        System.out.println(proId_processDescribe);
        System.out.println(shenuser);


        //总表pojo
        AoaProcessList process = new AoaProcessList();

        //当前用户
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");

        //出差pojo
        AoaEvection evection = new AoaEvection();


        //审核表
        AoaReviewed reviewed = new AoaReviewed();




        /*
            附件上传
        */

        if (file.getSize() != 0) {
            // 文件上传并获取 url
            String url = oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file));
            // String url =  oosUtil.getUrl(oosUtil.uploadImg2Oss(file));
            String shuffix = oosUtil.getPicSuffix(file.getOriginalFilename());
            long size = file.getSize();
            String type = file.getContentType();
            AoaAttachmentList attachment = new AoaAttachmentList();
            attachment.setAttachmentName(file.getOriginalFilename());
            attachment.setAttachmentPath(url);
            attachment.setAttachmentSize(size + "");
            attachment.setAttachmentType(type);
            attachment.setAttachmentShuffix(shuffix);
            attachment.setModel("note");
            attachment.setUploadTime(DateUtils.getDateTimeString());
            attachment.setUserId(aoaUser.getUserId() + "");
            newProcessService2.inertAttachment(attachment);
            System.out.println(attachment.getAttachmentId());
            process.setProFileId(attachment.getAttachmentId());
            System.out.println("ppID = " + process.getProFileId());

        }



        /*
         表单插入
        */

        //总表
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setDeeplyId(Integer.parseInt(proId_deeply));
        process.setEndTime(proId_endTime);
        process.setProcessName(proId_processName);
        process.setProcessDes(proId_processDescribe);
        process.setIsChecked(0);
        process.setStartTime(proId_startTime);
        process.setStatusId(23);
        process.setTypeName("出差申请");
        process.setProcessUserId(aoaUser.getUserId());
        process.setShenuser(shenuser);
        newProcessService2.insertProcess(process);


        //出差表
        evection.setProId(process.getProcessId());
        evection.setTypeId(Integer.parseInt(typeId));
        newProcessService2.insertEvection(evection);


        //审批表
        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());
        String[] shenuserName = shenuser.split(";");

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }


        return "redirect:/xinxeng";
    }


      /*
 ##############################   3.加班    ################################
 */

    @RequestMapping("overtime")
    public String overTime(Model model, HttpSession session) {
        //  紧急程度
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");
        model.addAttribute("harrylist", status);


        //用户名
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());

        //Type
        List<AoaTypeList> status2 = newProcessService2.queryType("加班");
        model.addAttribute("overtype", status2);


        //通讯录
        addressList(model, session);

        return "process/overtime";

    }


    @RequestMapping("over")
    public String over(HttpSession session,
                       String proId_processName,
                       String proId_deeply,
                       String typeId,
                       String proId_startTime,
                       String proId_endTime,
                       String proId_processDescribe,
                       String shenuser
    ) {
        System.out.println(proId_processName);
        System.out.println(proId_deeply);
        System.out.println(typeId);
        System.out.println(proId_startTime);
        System.out.println(proId_endTime);
        System.out.println(proId_processDescribe);
        System.out.println(shenuser);


        AoaProcessList process = new AoaProcessList();
        AoaOvertime overtime = new AoaOvertime();
        AoaReviewed reviewed = new AoaReviewed();
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        //总表
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setDeeplyId(Integer.parseInt(proId_deeply));
        process.setEndTime(proId_endTime);
        process.setProcessName(proId_processName);
        process.setProcessDes(proId_processDescribe);
        process.setIsChecked(0);
        process.setStartTime(proId_startTime);
        process.setStatusId(23);
        process.setTypeName("加班申请");
        //process.setShenuser(proId_shenuser);
        process.setProcessUserId(aoaUser.getUserId());
        process.setShenuser(shenuser);

        newProcessService2.insertProcess(process);


        // overtime
        overtime.setTypeId(Integer.parseInt(typeId));
        overtime.setProId(process.getProcessId());
        newProcessService2.insertOvertime(overtime);


        //审批表
        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());
        String[] shenuserName = shenuser.split(";");

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }


        return "redirect:/xinxeng";
    }
















 /*
    #########################      4.转正    ###################################
 */


    @RequestMapping("regular")
    public String regular(Model model, HttpSession session) {
        // 状态
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");

        //重要程度
        model.addAttribute("harrylist", status);

        //通讯录
        addressList(model, session);

        //获取当前用户，用户名输入到页面
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());

        return "process/regular";
    }


    /*
      @Description :  转正申请
      @Date   :  2021/11/23
      @Param  : [user]
      @return : java.lang.String
     */
    @RequestMapping("regu")
    private String regu(PositiveUser user, HttpSession session) {

        System.out.println(user);
       /*
            process表：
               processName=小李转正, deeplyId=22, startTime=null, endTime=2021-11-23 09:01:52,
               processUserId=null,shenuser=张天怿;黄熙锋

        */
        //总表
        AoaProcessList process = new AoaProcessList();
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setDeeplyId(user.getDeeplyId());
        process.setEndTime(user.getEndTime());
        process.setProcessName(user.getProcessName());
        process.setProcseeDays(DateUtils.between_days(user.getEndTime(), user.getStartTime()).intValue());
        process.setIsChecked(0);
        process.setStatusId(23);
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        process.setProcessUserId(aoaUser.getUserId());
        process.setStartTime(user.getStartTime());
        process.setTypeName("转正申请");
        process.setShenuser(user.getShenuser());

        //获取process主键
        newProcessService2.insertProcess(process);

        System.out.println("processId====" + process.getProcessId());



       /*

        regular：表
            understand=职责理解, experience=心得, pullulate=成长, deficiency=不足, dobetter=做的更好, advice=建议加薪)

        */
        //转正表
        AoaRegular regular = new AoaRegular();
        regular.setProId(process.getProcessId());
        regular.setExperience(user.getExperience());
        regular.setAdvice(user.getAdvice());
        regular.setUnderstand(user.getUnderstand());
        regular.setPullulate(user.getPullulate());
        regular.setDeficiency(user.getDeficiency());
        regular.setDobetter(user.getDobetter());
        regular.setDays(DateUtils.between_days(user.getStartTime(), user.getEndTime()).intValue());
        newProcessService2.insertRegular(regular);


        AoaReviewed reviewed = new AoaReviewed();


        //审批表
        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());
        String[] shenuserName = user.getShenuser().split(";");

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }

        return "redirect:/xinxeng";
    }


/*
 ############################     5.请假      #####################################
 */

    @RequestMapping("holiday")
    public String holiday(Model model, HttpSession session) {
        //  紧急程度
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");
        model.addAttribute("harrylist", status);

        //Type
        List<AoaTypeList> status2 = newProcessService2.queryType("请假");
        model.addAttribute("overtype", status2);

        //用户名
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());


        //通讯录
        addressList(model, session);


        return "process/holiday";
    }


    //请假表单
    @RequestMapping("holi")
    public String holi(AoaHoliday holiday, HttpSession session,
                       String proId_deeply,
                       String proId_processName,
                       String proId_startTime,
                       String proId_endTime,
                       String proId_shenuser,
                       String proId_processDescribe,
                       @RequestParam("filePath") MultipartFile file     //证明
    ) {
        System.out.println(holiday);
        System.out.println(proId_deeply);
        System.out.println(proId_processName);
        System.out.println(proId_startTime);
        System.out.println(proId_endTime);
        System.out.println(proId_shenuser);
        System.out.println(proId_processDescribe);


        //获取用户
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        //总表
        AoaProcessList process = new AoaProcessList();




         /*

         附件上传

        */
        if (file.getSize() != 0) {
            //url
            String url = oosUtil.getImgUrl(oosUtil.uploadImg2Oss(file));
            String shuffix = oosUtil.getPicSuffix(file.getOriginalFilename());
            long size = file.getSize();
            String type = file.getContentType();
            AoaAttachmentList attachment = new AoaAttachmentList();
            attachment.setAttachmentName(file.getOriginalFilename());
            attachment.setAttachmentPath(url);
            attachment.setAttachmentSize(size + "");
            attachment.setAttachmentType(type);
            attachment.setAttachmentShuffix(shuffix);
            attachment.setModel("note");
            attachment.setUploadTime(DateUtils.getDateTimeString());
            attachment.setUserId(aoaUser.getUserId() + "");
            newProcessService2.inertAttachment(attachment);
            System.out.println(attachment.getAttachmentId());
            process.setProFileId(attachment.getAttachmentId());
            System.out.println("ppID = " + process.getProFileId());

        }


        process.setProcessName(proId_processName);
        process.setDeeplyId(Integer.parseInt(proId_deeply));
        process.setStartTime(proId_startTime);
        process.setEndTime(proId_endTime);
        process.setShenuser(proId_shenuser);
        process.setProcessDes(proId_processDescribe);
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setStatusId(23);
        process.setTypeName("请假申请");
        process.setIsChecked(0);
        process.setProcseeDays(holiday.getLeaveDays());

        process.setProcessUserId(aoaUser.getUserId());
        process.setShenuser(proId_shenuser);

        newProcessService2.insertProcess(process);
        System.out.println("id====" + process.getProcessId());


        //请假
        holiday.setProId(process.getProcessId());
        newProcessService2.insertHoliday(holiday);


        AoaReviewed reviewed = new AoaReviewed();
        //审批表
        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());
        String[] shenuserName = proId_shenuser.split(";");

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }


        //holidayId=null, leaveDays=0, typeId=37, proId=null, managerAdvice=null, personnelAdvice=null)


        return "redirect:/xinxeng";

    }


/*
 ##########################     离职      #######################################
 */

    @RequestMapping("resign")
    private String resign(Model model, HttpSession session) {

        //  紧急程度
        List<AoaTypeList> status = newProcessService2.queryType("紧急程度");
        model.addAttribute("harrylist", status);


        //用户名
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        model.addAttribute("username", aoaUser.getUserName());


        //通讯录
        addressList(model, session);
        return "process/resign";
    }


    @RequestMapping("res")
    public String res(AoaResign resign, HttpSession session,
                      String proId_processName,
                      String proId_deeply,
                      String proId_processDescribe,
                      String finish,
                      String shenuser,
                      String handuser) {

        System.out.println(shenuser);

        System.out.println(resign);


        System.out.println(proId_processName);
        System.out.println(proId_deeply);
        System.out.println(proId_processDescribe);
        System.out.println(finish);
        System.out.println(shenuser);

        //总表
        AoaProcessList process = new AoaProcessList();
        process.setProcessName(proId_processName);
        process.setDeeplyId(Integer.parseInt(proId_deeply));

        process.setStartTime(DateUtils.getDateTimeString());

        process.setProcessDes(proId_processDescribe);
        process.setApplyTime(DateUtils.getDateTimeString());
        process.setStatusId(23);
        process.setTypeName("离职申请");
        process.setIsChecked(0);
        AoaUser aoaUser = (AoaUser) session.getAttribute("user");
        process.setProcessUserId(aoaUser.getUserId());
        process.setShenuser(shenuser);

        int id = newProcessService2.insertProcess(process);

        System.out.println(process.getProcessId());
        //查询用户id
        int userId = newProcessService2.queryUserIdByName(handuser);

        resign.setIsFinish(finish == null ? 0 : 1);
        resign.setHandUser(userId);

        resign.setProId(process.getProcessId());
        int i = newProcessService2.insertResign(resign);
        System.out.println("i = " + i);

        AoaReviewed reviewed = new AoaReviewed();


        reviewed.setAdvice("");
        reviewed.setReviewedTime(DateUtils.getDateTimeString());
        reviewed.setStatusId(23);
        reviewed.setProId(process.getProcessId());
        String[] shenuserName = shenuser.split(";");

        for (int j = 0; j < shenuserName.length; j++) {
            int shenuserId = newProcessService2.queryUserIdByName(shenuserName[j]);
            reviewed.setUserId(shenuserId);
            newProcessService2.insertReviewed(reviewed);
        }


        return "redirect:/xinxeng";

    }


    /*
      @Description : 第一次通讯录
      @Date   :  2021/11/24
      @Param  : [model]
      @return : void
     */
    public void addressList(Model model, HttpSession session) {

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");

        /*
              通讯录
        */
        int page = 1, size = 5;
        String baseKey = "";
        //计数
        int totalElements = newProcessDao2.findUserInfoByName(baseKey, aoaUser.getUserId(), page, 0, aoaUser.getRoleId()).size();
        //页数
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;
        List<AoaUser> users = newProcessDao2.findUserInfoByName(baseKey, aoaUser.getUserId(), page, size, aoaUser.getRoleId());
        Map<String, Object> mapPage = new HashMap<>();
        mapPage.put("totalElements", totalElements);
        mapPage.put("totalPages", totalPages);
        mapPage.put("number", page);
        mapPage.put("size", size);
        model.addAttribute("page", mapPage);
        model.addAttribute("url", "recivers");
        model.addAttribute("userInfo", users);
    }


    /*
   @Description : 通讯录
   @Date   :  2021/11/24
   @Param  : [model, baseKey, page, size]
   @return : java.lang.String
  */
    @RequestMapping("recivers")
    public String reciver(Model model, HttpSession session,
                          @RequestParam(value = "baseKey", defaultValue = "") String baseKey,
                          @RequestParam(value = "page", defaultValue = "1") int page,
                          @RequestParam(value = "size", defaultValue = "5") int size) {

        AoaUser aoaUser = (AoaUser) session.getAttribute("user");


        int totalElements = newProcessDao2.findUserInfoByName(baseKey, aoaUser.getUserId(), page, 0, aoaUser.getRoleId()).size();
        int totalPages = totalElements % size == 0 ? totalElements / size : totalElements / size + 1;

        List<AoaUser> userInfos = newProcessDao2.findUserInfoByName(baseKey, aoaUser.getUserId(), (page - 1) * size, size, aoaUser.getRoleId());
        model.addAttribute("userInfo", userInfos);

        Map<String, Object> mapPage = new HashMap<>();
        mapPage.put("totalElements", totalElements);
        mapPage.put("totalPages", totalPages);
        mapPage.put("number", page);
        mapPage.put("size", size);
        model.addAttribute("page", mapPage);
        model.addAttribute("url", "recivers");
        return "common/recivers";

    }

}
